<?php


namespace app\Utils;

use app\admin\model\System\SysSettingModel;
use think\Config;
use think\facade\Filesystem;
use think\facade\Request;
use think\File;
use OSS\OssClient;

class ImageUtil
{

    protected $setting;

    public function __construct()
    {
        $this->setting = SysSettingModel::getSysSettingWhere([['id', '=', 1]]);
    }

    public static function i()
    {
        $imageUtil = new ImageUtil();
        return $imageUtil;
    }

    /**
     * 上传图片文件(多图/单图)
     * @param $file
     * @param $path
     * @return string
     */
    public function uploads($file, $path)
    {
        if (is_array($file)) {
            $urls = [];
            foreach ($file as $item) {
                $url = Filesystem::disk('public')->putFile($path, $item);
                array_push($urls, "/uploads/" . $url);
            }
        } else {
            $url = Filesystem::disk('public')->putFile($path, $file);
            $urls = "/uploads/" . $url;
        }
        return $urls;
    }

    /**
     * 图片域名
     * 拼接替url换域名
     * @param $url
     * @return string
     */
    public function replaceUrl($url)
    {
        if ($url) {
            //从数据库配置里面查
            $uploadsUrl = $this->setting['img_url'];
            $url = (strstr($url, 'http') !== false || strstr($url, 'https') !== false) ? $url : $uploadsUrl . $url;
        } else {
            $url = "";
        }
        $url = str_replace('\\', '/', $url);
        return $url;
    }

    /**
     * 拼接主域名
     * @param $url
     * @return string
     */
    public function replaceSiteUrl($url)
    {
        if ($url) {
            //从数据库配置里面查
            $siteUrl = "";
            $url = (strstr($url, 'http') !== false || strstr($url, 'https') !== false) ? $url : $siteUrl . $url;
        } else {
            $url = "";
        }
        return $url;
    }

    /**
     * 所有域名替换
     * url替换成https
     * @param $url
     * @return string
     */
    public function replaceHttpsUrl($url)
    {
        if (strstr($url, 'https') == false) {
            $url = str_replace('http', 'https', $url);
        }
        return $url;
    }

    /**
     * 裁剪url
     * @param $url
     * @return string
     */
    public function cute_img_url($url, $type, $w, $h)
    {
        $w = $w ? intval($w) : '-';
        $h = $h ? intval($h) : '-';
        if (strstr($url, '/u1/') && in_array($type, ['c', 'r']) && $w && $h) {
            $size = '/u1/' . $type . $w . 'x' . $h . '/';
            $url = str_replace('/u1/', $size, $url);
        }
        return $url;
    }

    /**
     * @param File $files
     * @return string
     * 图片上传
     */
    public function upload($files, $path)
    {
        $object = $path . sha1(date('YmdHis', time()) . uniqid()) . '.png';
        try {
            $ossClient = new OssClient(config('app.aliyun.accessId'), config('app.aliyun.accessSecret'), config('app.aliyun.endpoint'));
            if (is_array($files)) {
                $urls = [];
                foreach ($files as $file) {
                    $result = $ossClient->uploadFile(config('app.aliyun.bucket'), $object, $file->getRealPath());
                    array_push($urls, $result['info']['url']);
                }
            } else {
                $result = $ossClient->uploadFile(config('app.aliyun.bucket'), $object, $files->getRealPath());
                $urls = $result['info']['url'];
            }
            return $urls;
        } catch (\Exception $e) {
            return $e->getMessage();
        }
    }
}
